<?
$version = "1.2";

function buyPayPal($aPrice,$aItemNumber,$aItemName,$aCurrencyCode,$aFormType,$aReturnURL,$aAccountNB)
{
	global $set_test_mode;
	if ($set_test_mode)
	{
		$form_url = "https://www.sandbox.paypal.com/cgi-bin/webscr/";	
		$array["ipn_test"] = "1";
	}
	else 
		$form_url = "https://www.paypal.com/cgi-bin/webscr/";	
	$array["cmd"] = "_xclick";
	$array["amount"] = $aPrice;
	$array["item_name"] = urlencode($aItemName);
	$array["item_number"] = $aItemNumber;
	$array["business"] = $aAccountNB;
	$array["currency_code"] = "$aCurrencyCode";
	$array["notify_url"] = $aReturnURL;
	$array["return"] = $aReturnURL;
	$array["rm"] = "2";
	$array["no_shipping"] = "1";
	
	return payForm($form_url,$array,$aFormType,$aReturnURL,$aAccountNB);
}

function buy2CheckOut($aPrice,$aItemNumber,$aItemName,$aCurrencyCode,$aFormType,$aReturnURL,$aAccountNB)
{
	global $set_test_mode;
	
	if ($set_test_mode)
		$array["demo"] = "Y";
	
	$form_url = "https://www2.2checkout.com/2co/buyer/purchase/";	
	$array["sid"] = $aAccountNB;
	$array["cart_order_id"] = $aItemNumber;
	$array["total"] = $aPrice;
	$array["item_name"] = $aItemName;
	
	return payForm($form_url,$array,$aFormType,$aReturnURL,$aAccountNB);
}

function buyWorldPay($aPrice,$aItemNumber,$aItemName,$aCurrencyCode,$aFormType,$aReturnURL,$aAccountNB,$aInstId)
{
	$form_url = "https://select.worldpay.com/wcc/purchase/";	
	$array["instId"] = $aInstId;
	$array["cartId"] = $aAccountNB;
	$array["M_item"] = $aItemNumber;
	$array["amount"] = $aPrice;
	$array["desc"] = $aItemName;
	$array["currency"] = "$aCurrencyCode";
	$array["M_pay"] = 1;
	$array["M_url"] = $aReturnURL;
	
	return payForm($form_url,$array,$aFormType,$aReturnURL,$aAccountNB);
}

function buyAuthorize($aPrice,$aItemNumber,$aItemName,$aCurrencyCode,$aFormType,$aReturnURL,$aAccountNB)
{
	$form_url = "https://secure.authorize.net/gateway/transact.dll";	
	$array["x_login"] = $aAccountNB;
	$array["idno"] = $aItemNumber;
	$array["x_amount"] = $aPrice;
	$array["x_description"] = $aItemName;
	$array["x_relay_response"] = "Yes";
	$array["x_currency_code"] = "$aCurrencyCode";
	$array["x_show_form"] = "PAYMENT_FORM";
	$array["x_relay_url"] = $aReturnURL;
	
	return payForm($form_url,$array,$aFormType,$aReturnURL,$aAccountNB);
}

function buyBeanStream($aPrice,$aItemNumber,$aItemName,$aCurrencyCode,$aFormType,$aReturnURL,$aAccountNB)
{
	$form_url = "https://www.beanstream.com/scripts/payment/payment.asp";	
	$array["merchant_id"] = $aAccountNB;
	$array["trnOrderNumber"] = $aItemNumber;
	$array["trnAmount"] = $aPrice;
	$array["approvedPage"] = $aReturnURL;
	$array["declinedPage"] = $aReturnURL;
	
	return payForm($form_url,$array,$aFormType,$aReturnURL,$aAccountNB);
}

function buySecPay($aPrice,$aItemNumber,$aItemName,$aCurrencyCode,$aFormType,$aReturnURL,$aAccountNB)
{
	global $set_test_mode;
	
	if ($set_test_mode)
		$array["test_status"] = "true";
	
	$form_url = "https://www.secpay.com/java-bin/ValCard";	
	$array["merchant"] = $aAccountNB;
	$array["trans_id"] = $aItemNumber;
	$array["amount"] = $aPrice;
	$array["callback"] = $aReturnURL;
	$array["currency"] = $aCurrencyCode;
	$array["options"] = "cb_post=true";

	
	
	return payForm($form_url,$array,$aFormType,$aReturnURL,$aAccountNB);
}

function buySecTrading($aPrice,$aItemNumber,$aItemName,$aCurrencyCode,$aFormType,$aReturnURL,$aAccountNB)
{
	$form_url = "https://securetrading.net/authorize/process.cgi";	
	$array["merchant"] = $aAccountNB;
	$array["orderref"] = $aItemNumber;
	$aPrice=round($aPrice)*100;
	$array["amount"] = $aPrice;
	$array["callbackurl"] = 1;
	$array["url1"] = 1;
	$array["url2"] = 2;
	$array["failureurl"] = 2;
	$array["method"] = "post";
	$array["currency"] = $aCurrencyCode;
	
	return payForm($form_url,$array,$aFormType,$aReturnURL,$aAccountNB);
}

function buyNochex($aPrice,$aItemNumber,$aItemName,$aCurrencyCode,$aFormType,$aReturnURL,$aAccountNB)
{
	$form_url = "https://secure.nochex.com";	
	$array["order_id"] = $aItemNumber;
	$array["amount"] = $aPrice;
	$array["description"] = $aItemName;
	$array["success_url"] = $aReturnURL;
	$array["cancel_url"] = $aReturnURL;
	$array["declined_url"] = $aReturnURL;
	$array["status"] = "live";
	$array["email_address"] = "$aAccountNB";
	$array["currency"] = $aCurrencyCode;
	$array["merchant_id"] = $aAccountNB;
	 
	
	return payForm($form_url,$array,$aFormType,$aReturnURL,$aAccountNB);
}




function getReturnGateway()
{
	if (isset($_REQUEST["item_number"]))
		return getPayPalData();
	elseif (isset($_REQUEST["cart_order_id"]))
		return get2CheckOutData();
	elseif (isset($_REQUEST["M_item"]))
		return getWorldPayData();
	elseif (isset($_REQUEST["idno"]))
		return getAuthData();
	elseif (isset($_REQUEST["trnId"]))
		return getBeanStreamData();
	elseif (isset($_REQUEST["trans_id"]))
		return getSecPayData();
	elseif (isset($_REQUEST["orderref"]))
		return getSecTradingData();
	elseif (isset($_REQUEST["order_id"]))
		return getNochexData();
	else 
		return "None of the REQUEST variables for identify gateway found.";
}

function getPayPalData()
{
	$item_number = $_REQUEST['item_number'];
	$payment_status = $_REQUEST['payment_status'];
	$payment_amount = $_REQUEST['mc_gross'];
	
	if ($payment_status=="Completed")
		$pay_status = 10;
	elseif ($payment_status=="Denied")
		$pay_status = 20;
	elseif ($payment_status=="Pending")
		$pay_status = 30;
	elseif ($payment_status=="Reversed")
		$pay_status = 99;
	
		
	return logPayHandler("$item_number","$payment_amount",1,$pay_status);	
}

function get2CheckOutData()
{
	$total = $_REQUEST["total"];
	$cart_order_id = $_REQUEST["cart_order_id"];
	$credit_card_processed = $_REQUEST["credit_card_processed"];

	if ($credit_card_processed=="Y")
		$pay_status = 10;
	elseif ($credit_card_processed=="K")
		$pay_status = 30;
	
	return logPayHandler("$cart_order_id","$total",2,$pay_status);
}

function getWorldPayData()
{
	$total = $_POST["authAmount"];
	$item_id = $_POST["M_item"];
	$transStatus = $_POST["transStatus"];
	
	if ($transStatus=="Y")
		$pay_status = 10;
	elseif ($transStatus=="K")
		$pay_status = 30;
	
	return logPayHandler("$item_id","$total",3,$pay_status);
}

function getAuthData()
{
	$x_response_code  = $_POST["x_response_code"];
	$txn_id  = $_POST["x_trans_id"];
	$idno  = $_POST["idno"];
	$total  = $_POST["x_amount"];
		
	if ($x_response_code=="Y")
		$pay_status = 10;
	elseif ($x_response_code=="K")
		$pay_status = 30;
	
	return logPayHandler("$idno","$total",4,$pay_status);
}

function getBeanStreamData()
{
	$messageText  = $_GET["messageText"];
	$idno  = $_GET["trnOrderNumber"];
	$total  = $_GET["trnAmount"];
	if (isset($_REQUEST["errorMessage"]))
		$messageText = $_REQUEST["errorMessage"];
		
		
	if ($messageText=="Approved")
		$pay_status = 10;
	else
		$pay_status = 20;

	return logPayHandler("$idno","$total",6,$pay_status);
}

function getSecPayData()
{
	$code  = $_REQUEST["code"];
	$idno  = $_REQUEST["trans_id"];
	$total = $_REQUEST["amount"];
		
	if ($code=="A")
		$pay_status = 10;
	elseif ($code=="N")
		$pay_status = 20;
	
	return logPayHandler("$idno","$total",7,$pay_status);
}

function getSecTradingData()
{
	$code  = $_REQUEST["stauthcode"];
	$idno  = $_REQUEST["orderref"];
	$total  = $_REQUEST["amount"];

	if ($code=="AUTH CODE:TEST")
		$pay_status = 10;
	else
		$pay_status = 20;

	return logPayHandler("$idno","$total",8,$pay_status);
}
function getNochexData()
{
	$code  = $_REQUEST["status"];
	$idno  = $_REQUEST["order_id"];
	$total  = $_REQUEST["amount"];
	
	if ($code=="test")  // To test, use $code="test"
		$pay_status = 10;
	else
		$pay_status = 20;

	return logPayHandler("$idno","$total",9,$pay_status);
}

function payForm($aFormURL,$aFieldArray,$aSubmitType)
{
	
	if ($aSubmitType==1 || $aSubmitType==2)
	{
		if ($aSubmitType==1)
			$method = "get";
		elseif ($aSubmitType==2)
			$method = "post"; 
			
		$form = "<form action='$aFormURL' method='$method'>\n";
		foreach ($aFieldArray as $key => $value) 
			$form.="<input type='hidden' name='$key' value='$value'>\n";
		$form.= "<input type='submit' name='submit' value='Order now!'>\n";
		$form.= "</form>\n";
		
		
	}
	elseif ($aSubmitType==3)
	{
		$form = $aFormURL . "?";
		foreach ($aFieldArray as $key => $value) 
			$form.="$key=$value&";
			
	}
	return $form;
	
}

function logPaySuccess($oid,$amount,$vendor)
{
	// REMOTE CALL:  Update set Paid
	return markPaid($oid,$amount,$vendor);
}
function logPayPending($oid,$amount,$vendor)
{
	// REMOTE CALL:  Update set Pending
	return markPending($oid,$amount,$vendor);
}
function logPayReversed($oid,$amount,$vendor)
{
	// REMOTE CALL:  Update set Reversed
	return markReversed($oid,$amount,$vendor);
}
function logPayInvalid($oid,$amount,$vendor)
{
	// REMOTE CALL:  Update set Invalid
	return markInvalid($oid,$amount,$vendor);
}
function checkValidIdAndAmount($oid,$amount)
{
	// REMOTE CALL:  Check that id and amount is correct
	return ValidIdAndAmount($oid,$amount);
}
function neverHappen($oid,$amount,$vendor)
{
	// REMOTE CALL:  Return a nice error message
	return payNeverHappen($oid,$amount);	
}

function logPayHandler($oid,$amount,$vendor,$pay_status)
{
	$oid = round($oid);
	
	if ($pay_status==10)
		return logPaySuccess($oid,$amount,$vendor);
	elseif ($pay_status==20)	
		return logPayInvalid($oid,$amount,$vendor);
	elseif ($pay_status==30)	
		return logPayPending($oid,$amount,$vendor);
	elseif ($pay_status==40 || $pay_status==50)	
		return logPayReversed($oid,$amount,$vendor);
	else 
		return neverHappen($oid,$amount,$vendor);
}
?>